package cn.kgc.springcloud.controller;

import cn.kgc.springcloud.entity.ResponseResult;
import cn.kgc.springcloud.entity.User;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

/**
 * @author 课工场
 * @date 2024/7/22
 * @description   /producer/m2?id=1
 */
@RestController
@RequestMapping("producer")
public class ProducerController {

    @Value("${server.port}")
    private String  port;

    @GetMapping("m1")
    public String method1() {

        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

        return "ProducerController service ok port :"+port;
    }


    //  /producer/m2?id=10
    @GetMapping("m2")
    public String method2(Integer id) {
        return "ProducerController service ok port :"+port+",id="+id;
    }

    // /producer/m3/10
    @GetMapping("m3/{id}")
    public String method3(@PathVariable("id") Integer id) {
        return "ProducerController service ok port :"+port+",id="+id;
    }


    @GetMapping("m4")
    public String method4(User user) {
        return "ProducerController service ok port :"+port+",user="+user;
    }


    @PostMapping("m5")
    public String method5(@RequestBody User user) {
        return "ProducerController service ok port :"+port+",user="+user;
    }

    // ?ids=1&ids=2&ids=3
    @PostMapping("m6")
    public String method6(Integer[] ids) {
        for (Integer id : ids) {
            System.out.println("id = " + id);
        }
        String params = Arrays.toString(ids);
        return "ProducerController service ok port :"+port+",ids="+params;
    }

    @GetMapping("m7")
    public ResponseResult method7() {

        ResponseResult responseResult = new ResponseResult();
        responseResult.setCode(200);
        responseResult.setMessage("ok");
        User user = new User();
        user.setId(1);
        user.setUsername("tom");
        user.setPassword("123456");
        responseResult.setData(user);

        return responseResult;
    }

}
